﻿@model AssociateProductToAttributeValueSearchModel

@{
    Layout = "_AdminPopupLayout";

    //page title
    ViewBag.PageTitle = T("Admin.Catalog.Products.ProductAttributes.Attributes.Values.Fields.AssociatedProduct.AddNew").Text;
}

@if (ViewBag.RefreshPage == true)
{
    <script>
        try {
            window.opener.$('#@(Context.Request.Query["productIdInput"])').val('@(ViewBag.productId)').trigger('change');
            window.opener.document.getElementById('@(Context.Request.Query["productNameInput"])').innerHTML = '@(ViewBag.productName)';
        }
        catch (e){}
        window.close();
    </script>
}

<form asp-controller="Product" asp-action="AssociateProductToAttributeValuePopup"
      asp-route-productIdInput="@Context.Request.Query["productIdInput"]"
      asp-route-productNameInput="@Context.Request.Query["productNameInput"]">
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.Catalog.Products.ProductAttributes.Attributes.Values.Fields.AssociatedProduct.AddNew")
        </h1>
        <div class="pull-right">
            &nbsp;
        </div>
    </div>

    <div class="content">
    <input asp-for="AssociateProductToAttributeValueModel.AssociatedToProductId" type="hidden" />
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search panel-popup">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-sm-6">
                                <div class="form-group">
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchProductName" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-editor asp-for="SearchProductName" />
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchCategoryId" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-select asp-for="SearchCategoryId" asp-items="Model.AvailableCategories" />
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableVendors.SelectionIsNotPossible() || Model.IsLoggedInAsVendor ? Html.Raw("style='display: none;'") : null)>
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchVendorId" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-select asp-for="SearchVendorId" asp-items="Model.AvailableVendors" />
                                    </div>
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <div class="form-group" @(Model.AvailableStores.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchStoreId" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-select asp-for="SearchStoreId" asp-items="Model.AvailableStores" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchProductTypeId" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-select asp-for="SearchProductTypeId" asp-items="Model.AvailableProductTypes" />
                                    </div>
                                </div>
                                <div class="form-group" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                    <div class="col-sm-5">
                                        <nop-label asp-for="SearchManufacturerId" />
                                    </div>
                                    <div class="col-sm-7">
                                        <nop-select asp-for="SearchManufacturerId" asp-items="Model.AvailableManufacturers" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-12">
                                <button type="button" id="search-products" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        @await Html.PartialAsync("Table", new DataTablesModel
                        {
                            Name = "products-grid",
                            UrlRead = new DataUrl("AssociateProductToAttributeValuePopupList", "Product", null),
                            SearchButtonId = "search-products",
                            Length = Model.PageSize,
                            LengthMenu = Model.AvailablePageSizes,
                            Filters = new List<FilterParameter>
                            {
                                new FilterParameter(nameof(Model.SearchProductName)),
                                new FilterParameter(nameof(Model.SearchCategoryId)),
                                new FilterParameter(nameof(Model.SearchManufacturerId)),
                                new FilterParameter(nameof(Model.SearchStoreId)),
                                new FilterParameter(nameof(Model.SearchVendorId)),
                                new FilterParameter(nameof(Model.SearchProductTypeId))
                            },
                            ColumnCollection = new List<ColumnProperty>
                            {
                                new ColumnProperty(nameof(ProductModel.Id))
                                {
                                    Title = T("Admin.Common.Select").Text,
                                    Width = "50",
                                    Render = new RenderButtonCustom(NopButtonClassDefaults.Default, T("Admin.Common.Select").Text)
                                    {
                                        OnClickFunctionName = "selectAssociatedProduct"
                                    }
                                },
                                new ColumnProperty(nameof(ProductModel.Name))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Name").Text
                                },
                                new ColumnProperty(nameof(ProductModel.Published))
                                {
                                    Title = T("Admin.Catalog.Products.Fields.Published").Text,
                                    Width = "100",
                                    ClassName = NopColumnClassDefaults.CenterAll,
                                    Render = new RenderBoolean()
                                }
                            }
                        })
                    </div>
                    <div class="panel-footer" style="display: none">
                        <button type="submit" id="save" name="save" class="btn btn-primary">
                            @T("Admin.Common.Save")
                        </button>
                    </div>

                    <script>
                        function selectAssociatedProduct(productid) {
                            $("#@Html.IdFor(model => model.AssociateProductToAttributeValueModel.AssociatedToProductId)").val(productid);
                            $('#save').click();
                        }
                    </script>
                </div>
            </div>
        </div>
    </div>
</form>